package com.lujianfei.recyclerview.recyclerview_multi_type;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;

import com.lujianfei.recyclerview.R;
import com.lujianfei.recyclerview.adapter.WaterfallBannerAdapter;
import com.lujianfei.recyclerview.entity.ListItem;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

public class WaterfallBannerSpacingActivity extends AppCompatActivity {

    RecyclerView recyclerview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view_vertical);
        recyclerview = findViewById(R.id.recyclerview);
        /** 设置列表布局方式 */
        StaggeredGridLayoutManager layoutManager= new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recyclerview.addItemDecoration(new WaterfallItemDecoration());
        recyclerview.setLayoutManager(layoutManager);
        List<ListItem> listItems = genData();
        WaterfallBannerAdapter waterfallAdapter = new WaterfallBannerAdapter(listItems);
        recyclerview.setAdapter(waterfallAdapter);
    }

    private List<ListItem> genData() {
        List<ListItem> listItems = new ArrayList<>();
        int[] color = new int[]{0xffff0000,0xff00ff00,0xff0000ff,0xffffff00};
        listItems.add(new ListItem(WaterfallBannerAdapter.TYPE_ITEM_BANNER,"Banner","Summary",color[2]));
        for (int i = 0; i < 20; i++) {
            listItems.add(new ListItem(WaterfallBannerAdapter.TYPE_ITEM,"Title "+i,"Summary "+i,color[i % 4]));
        }
        return listItems;
    }

    class WaterfallItemDecoration extends RecyclerView.ItemDecoration {

        final int top_spacing = 20;
        final int left_right_spacing = 20;
        final int middle_spacing = 20;
        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            int childAdapterPosition = parent.getChildAdapterPosition(view);
            StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
            outRect.top = top_spacing;
            if (0 == childAdapterPosition) {
                // 头视图间距控制
                outRect.left = left_right_spacing;
                outRect.right = left_right_spacing;
            } else {
                // 瀑布流间距控制
                // 获取item在span中的下标，决定瀑布流左右位置的index
                int spanIndex = params.getSpanIndex();
                if (0 == spanIndex) {
                    outRect.left = left_right_spacing;
                    outRect.right = middle_spacing / 2;
                } else if (1 == spanIndex) {
                    outRect.left = middle_spacing / 2;
                    outRect.right = left_right_spacing;
                }
            }
        }
    }
}
